$(function () {
    var timer = null;

    //鬼子移动
    function Tank() {
        this.enemyMove = function (n) {
            $("body").append($("<div class='enemy'></div>"))
            this.tank = $(".enemy:eq(" + n + ")");
            this.tank.attr("type", "鬼子");
            var that = this;
            var enemyDir = Math.ceil(Math.random() * 4);
            var step = 0;
            var html = document.documentElement;
            var enemy = setInterval(function () {
                var enemyTop = parseInt(that.tank.css("top"));
                var enemyLeft = parseInt(that.tank.css("left"));
                step++;
                if (step == 5) {
                    enemyDir = Math.ceil(Math.random() * 4);
                    step = 0;
                }
                switch (true) {
                    case enemyDir == 1:
                        that.tank.css({
                            left: enemyLeft -= 50,
                            transform: "rotate(-90deg)"
                        }).attr("enemydir", 1);
                        break;
                    case enemyDir == 2:
                        that.tank.css({
                            top: enemyTop -= 50,
                            transform: "rotate(0deg)"
                        }).attr("enemydir", 2);
                        break;
                    case enemyDir == 3:
                        that.tank.css({
                            left: enemyLeft += 50,
                            transform: "rotate(90deg)"
                        }).attr("enemydir", 3);
                        break;
                    case enemyDir == 4:
                        that.tank.css({
                            top: enemyTop += 50,
                            transform: "rotate(180deg)"
                        }).attr("enemydir", 4);
                        break;
                }
                switch (true) {
                    case that.tank.offset().left < 0:
                        that.tank.css({
                            left: 0
                        });
                        break;
                    case that.tank.offset().top < 0:
                        that.tank.css({
                            top: 0
                        });
                        break;
                    case that.tank.offset().left + that.tank.width() >= 1600:
                        that.tank.css({
                            left: 1550
                        });
                        break;
                    case that.tank.offset().top + that.tank.height() >= 757:
                        that.tank.css({
                            top: 700
                        });
                        break;
                }
                if (enemyDir == 2 && $('.mine').offset().top + $(".mine").height() < that.tank.offset().top && $('.mine').offset().left + $(".mine").width() <= that.tank.offset().left + that.tank.width() && $('.mine').offset().left + $(".mine").width() >= that.tank.offset().left) {
                    //炮弹运行轨迹
                    var fight = new Egg();
                    fight.chicken(2, enemyLeft, enemyTop, $(".enemy").attr("type"), $(".mine").attr("type"));

                } else if (enemyDir == 1 && $('.mine').offset().left + $(".mine").width() <= that.tank.offset().left && $('.mine').offset().top <= that.tank.offset().top + that.tank.height() && $('.mine').offset().top + $(".mine").height() >= that.tank.offset().top) {
                     //炮弹运行轨迹
                    var fight = new Egg();
                    fight.chicken(1, enemyLeft, enemyTop, $(".enemy").attr("type"), $(".mine").attr("type"));

                } else if (enemyDir == 3 && $('.mine').offset().left >= that.tank.offset().left + that.tank.width() && $('.mine').offset().top <= that.tank.offset().top + that.tank.height() && $('.mine').offset().top + $(".mine").height() >= that.tank.offset().top) {
                     //炮弹运行轨迹
                    var fight = new Egg();
                    fight.chicken(3, enemyLeft, enemyTop, $(".enemy").attr("type"), $(".mine").attr("type"));

                } else if (enemyDir == 4 && $('.mine').offset().top >= that.tank.offset().top + that.tank.height() && $('.mine').offset().left <= that.tank.offset().left + that.tank.width() && $('.mine').offset().left + $(".mine").width() >= that.tank.offset().left) {
                     //炮弹运行轨迹
                    var fight = new Egg();
                    fight.chicken(4, enemyLeft, enemyTop, $(".enemy").attr("type"), $(".mine").attr("type"));
                }
            }, 300)
        }
    }



    //友军移动
    function MineMove(mine, document) {
        this.tank = mine;
        this.tank.attr("type", "红军");
        this.document = document;
    }
    MineMove.prototype.MineRun = function () {
        var that = this;
        this.document.on("keydown", function (ev) {
            var left = parseInt(that.tank.css("left"));
            var top = parseInt(that.tank.css("top"));
            if (ev.keyCode == 37 || ev.keyCode == 38 || ev.keyCode == 39 || ev.keyCode == 40) {
                clearInterval(timer);
                timer = setInterval(function () {
                    switch (true) {
                        case ev.keyCode == 37:
                            that.tank.css({
                                left: left -= 50,
                                transform: "rotate(180deg)"
                            }).attr("dir", 1);
                            break;
                        case ev.keyCode == 38:
                            that.tank.css({
                                top: top -= 50,
                                transform: "rotate(-90deg)"
                            }).attr("dir", 2);
                            break;
                        case ev.keyCode == 39:
                            that.tank.css({
                                left: left += 50,
                                transform: "rotate(0deg)"
                            }).attr("dir", 3);
                            break;
                        case ev.keyCode == 40:
                            that.tank.css({
                                top: top += 50,
                                transform: "rotate(90deg)"
                            }).attr("dir", 4);
                            break;
                    }
                }, 300);
            } else if (ev.keyCode == 32) {
                var paoDan = new Egg();
                paoDan.chicken($(".mine").attr("dir"), left, top, $(".mine").attr("type"), $(".enemy").attr("type"));
            }
        })
    }


    //炮弹向鬼子的身上射去
    function Egg() {
        this.html = document.documentElement;
        MineMove.call(this, $('.mine'), $(document));
    }
    //干死鬼子
    Egg.prototype.chicken = function (dir, tankLeft, tankTop, tankType, tankEnemyType) {
        $("body").append($("<div class='pd'></div>"));
        var pdLength = $(".pd").length;
        this.pd = $(".pd:eq(" + (pdLength - 1) + ")");
        this.pd.attr("type",tankType);
        var that = this;
        this.pd.css({
            left: tankLeft + 14,
            top: tankTop + 14
        })
        $(".pd:eq(" + (pdLength - 1) + ")").egg = setInterval(function () {
            var pdPositionLeft = parseInt(that.pd.css("left"));
            var pdPositionTop = parseInt(that.pd.css("top"));
            switch (true) {
                case dir == 1:
                    that.pd.css("left", pdPositionLeft -= 15);
                    break;
                case dir == 2:
                    that.pd.css("top", pdPositionTop -= 15);
                    break;
                case dir == 3:
                    that.pd.css("left", pdPositionLeft += 15);
                    break;
                case dir == 4:
                    that.pd.css("top", pdPositionTop += 15);
                    break;
            }
            //判断炮弹当前位置，如触碰到了边界，删除掉触碰边界的炮弹
            $(".pd").each(function (idx, item) {
                if ($(item).offset().top <= 5 || $(item).offset().left <= 5 || $(item).offset().top + $(item).height() > that.html.clientHeight || $(item).offset().left + $(item).width() >= that.html.offsetWidth) {
                    //删掉这个元素
                    $(item).remove();
                    //定时器清不掉
                    clearInterval(item.egg);
                }
            })
            that.killGuiZi(tankType,tankEnemyType);
        }, 30)
    }
    //判断炮弹是否打中敌人;
    Egg.prototype.killGuiZi = function (type,dieType) {
        if(type == "红军"){
            sure($(".pd"),$(".enemy"));
        }else{
            sure($(".pd"),$(".mine"));
        }
        function sure(open,kill) {
            // console.log(type);
            open.each(function (idx, item) {
                // console.log($(item).attr("type"));
                kill.each(function (idx1, item1) {
                    console.log($(item1).attr("type"));
                    var pdOffsetTop = $(item).offset().top;
                    var pdOffsetLeft = $(item).offset().left;
                    if (pdOffsetTop < $(item1).offset().top + $(item1).height() && pdOffsetTop + $(item).height() > $(item1).offset().top && pdOffsetLeft < $(item1).offset().left + $(item1).width() && pdOffsetLeft + $(item).width() > $(item1).offset().left && $(item).attr("type") != dieType) {
                        $(item).remove();
                        $(item1).remove();
                        clearInterval($(".pd:eq(" + idx + ")").egg);
                    }
                })
            })
        }
    }

    //鬼子的实例对象
    var frequency = 2;
    for (var i = 0; i < frequency; i++) {
        var play = new Tank();
        play.enemyMove(i);
    }

    //友军坦克的示例对象
    var redChine = new MineMove($('.mine'), $(document));
    redChine.MineRun();
    

})